package cn.edu.sjtu.literature.web.controller;

import java.text.ParseException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import cn.edu.sjtu.literature.entity.Category;
import cn.edu.sjtu.literature.entity.Literature;
import cn.edu.sjtu.literature.service.LiteratureQuery;
import cn.edu.sjtu.literature.web.util.Constants;
import cn.edu.sjtu.literature.web.view.View;

public class LiteratureQueryController extends AbstractController {

	@Override
	public View doAction(HttpServletRequest req, HttpServletResponse resp) {
		log.debug("do query literature");
		
		// add categories to view
		req.setAttribute(Constants.CATEGORIES, literatureService.getCategories());
		
		// validate and bind value
		LiteratureQuery query = validateAndBind(req);
		
		// query literature
		log.debug("validate field successfully and query literature");
		List<Literature> literatures = literatureService.queryLiteratures(query);
		log.debug("literatures size: " + literatures.size());
		req.setAttribute(Constants.LITERATURES, literatures);
		
		return getView(Constants.VIEW_LITERATURE_LIST);
	}

	private LiteratureQuery validateAndBind(HttpServletRequest req) {
		String issueNo = req.getParameter("issueNo");
		String title = req.getParameter("title");
		String categoryId = req.getParameter("categoryId");
		String author = req.getParameter("author");
		String authorFirm = req.getParameter("authorFirm");
		String authorContactInfo = req.getParameter("authorContactInfo");
		String source = req.getParameter("source");
		String publishDateFrom = req.getParameter("publishDateFrom");
		String publishDateTo = req.getParameter("publishDateTo");
		
		log.debug("issueNo: " + issueNo);
		log.debug("title: " + title);
		log.debug("categoryId: " + categoryId);
		log.debug("author: " + author);
		log.debug("authorFirm: " + authorFirm);
		log.debug("authorContactInfo: " + authorContactInfo);
		log.debug("source: " + source);
		log.debug("publishDateFrom: " + publishDateFrom);
		log.debug("publishDateTo: " + publishDateTo);
		
		LiteratureQuery query = new LiteratureQuery();
		query.setIssueNo(issueNo);
		query.setTitle(title);
		if (StringUtils.isNotBlank(categoryId)) {
			Category category = new Category();
			category.setId(Integer.parseInt(categoryId));
			query.setCategory(category);
		}
		query.setAuthor(author);
		query.setAuthorFirm(authorFirm);
		query.setAuthorContactInfo(authorContactInfo);
		query.setSource(source);
		if (StringUtils.isNotBlank(publishDateFrom)) {
			try {
				query.setPublishDateFrom(Constants.NORMAL_DATE_FORMAT.parse(publishDateFrom));
			} catch (ParseException e) {
				log.warn(e.getMessage(), e);
			}
		}
		if (StringUtils.isNotBlank(publishDateTo)) {
			try {
				query.setPublishDateTo(Constants.NORMAL_DATE_FORMAT.parse(publishDateTo));
			} catch (ParseException e) {
				log.warn(e.getMessage(), e);
			}
		}
		req.setAttribute(Constants.LITERATURE_QUERY, query);
		return query;
	}
	
}
